<?php

class games extends app
{
    const ERRCODE_LOGIN_EXPIRE = -1000; // 登录过期了,需要重新授权
    const ERRMSG_LOGIN_EXPIRE = '登录过期了,需要重新授权';
   //用户登录
    function regdate_member()
    {
        global $_G;
        $post = $_POST;
        if (isset($post['code']) && !empty($post['code'])
            && isset($post['encryptedData']) && !empty($post['encryptedData'])
            && isset($post['iv']) && !empty($post['iv'])) {
            // 第一次授权登录

            if (!class_exists('XiaoChengXu')) include_once ROOT_PATH . 'web/lib/wx_xiaochengxu/XiaoChengXu.class.php';


            $xiaochengxu = new XiaoChengXu(array('appid' => 'wx2eadef08fd69770a', 'appsecret' => '47a351202ec1d200a7ce75c4353dded1'));
            $info = $xiaochengxu->getUserInfo($post['code'], $post['encryptedData'], $post['iv']);
            $arr = array();
            $arr['username'] = emoji_encode($info['nickName']);
            $arr['zopenid'] = $info['openId'];
            $arr['gold'] = 100;
            $arr['sex'] = $info['gender'];
            $arr['picurl'] = $info['avatarUrl'];
            $arr['login_time'] = TIMESTAMP;
            $arr['login_name'] = 'xiaochengxu';
            $member_info = DB::fetch_first("select `uid`,`gold`,`allowplay` from " . DB::table('member') . " where zopenid='" . $arr['zopenid'] . "'");
            if (!empty($member_info['uid']) && isset($member_info['uid'])) {
                $r = DB::update('member', array('login_time' => TIMESTAMP), array('uid' => $member_info['uid']));
                if (!$r > 0) {
                    echo json_encode(array('r' => -1));
                    exit;
                }
                $ukey = $arr['login_name'] . '_' . $member_info['uid'];
                $third_session = authcode($ukey, 'encode', '', 7 * 24 * 3600);
                $info['kgame'] = $third_session;
                $info['uid'] = $member_info['uid'];
                $info['gold']=$member_info['gold'];
                $info['allowplay']=$member_info['allowplay'];
                memory('set', $ukey, $info, 7 * 24 * 3600);
                echo json_encode(array('info' => $info));
                exit;
            } else {
                $arr['regdate'] = TIMESTAMP;
                if (!empty($post['t_uid'])) {
                    $arr['t_uid'] = $post['t_uid'];
                    $arr['allowplay']=1;
                }
                $r = DB::insert("member", $arr, true);
                $member_infos = DB::fetch_first("select `uid`,`gold`,`allowplay` from " . DB::table('member') . " where uid='" . $r . "'");
                $ukey = $arr['login_name'] . '_' . $r;
                $third_session = authcode($ukey, 'encode', '', 7 * 24 * 3600);
                $info['kgame'] = $third_session;
                $info['uid'] = $r;
                $info['gold']=$member_infos['gold'];
                $info['allowplay']=$member_infos['allowplay'];
                memory('set', $ukey, $info, 7 * 24 * 3600);
                echo json_encode(array('info' => $info));
                exit;
            }
        }
    }

            //游戏列表
    function gamelist()
    {
        $post = $_POST;
        if (isset($post['kgame']) && !empty($post['kgame'])) {
            $third_session = $post['kgame'];
            $ukey = authcode($third_session, 'decode');
            if (empty($ukey)) {
                echo json_encode(array('r' => -1, 'errcode' => self::ERRCODE_LOGIN_EXPIRE, 'errmsg' => self::ERRMSG_LOGIN_EXPIRE));
                exit;
            }

            $info = memory('get', $ukey);
            if (!$info) {
                // 过期了，要重新微信授权登录
                memory('delete', $ukey);
                echo json_encode(array('r' => -1, 'errcode' => self::ERRCODE_LOGIN_EXPIRE, 'errmsg' => self::ERRMSG_LOGIN_EXPIRE));
                exit;
            }
          $gold=DB::fetch_first("select `gold`,`allowplay` from ".DB::table('member')." where uid=".$info['uid']);
            $info['allowplay']=$gold['allowplay'];
            $info['gold']=$gold['gold'];
            $game_list = DB::fetch_all("select * from " . DB::table('games'));
            if (empty($game_list)) {
                echo json_encode(array('r' => -1));
                exit;
            }

            echo json_encode(array('r' => 0, 'result' => $game_list, 'info' => $info));
            exit;
        }
    }

    function usedgold()
    {
        $post = $_POST;
        if (!empty($post['uid']) && !empty($post['usedgold'])) {
            $member_info = DB::fetch_first("select `gold`,`t_uid` from " . DB::table('member') . " where uid=" . $post['uid']);
            if ($member_info['gold'] > $post['usedgold'] && !empty($member_info) && isset($member_info)) {
                DB::query("update " . DB::table('member') . " set `gold`=`gold`-" . $post['usedgold'] . " where uid=" . $post['uid']);
            }
            if ($member_info['t_uid'] > 0) {
                $highergold_info = DB::fetch_first("select `uid`,`type` from " . DB::table('goldcoin') . " where uid=" . $member_info['t_uid']);
               if ('share_getgold'!=$highergold_info['type'] || empty($highergold_info)) {
                    DB::query("update " . DB::table('member') . " set `gold`=`gold`+100 where uid=" . $member_info['t_uid']);
                    $date = date("Y-m-d", TIMESTAMP);
                    $rec = DB::insert('goldcoin', array('uid' => $member_info['t_uid'], 'type' => 'share_getgold', 'goldcoin' => 100, 'desc' => '分享得的金币', 'date' => $date, 'dateline' => TIMESTAMP), true);
                    if (!$rec > 0) {
                        echo json_encode(array('r' => -1));
                        exit;
                    }
                }
            }
          echo json_encode(array('r'=>0));exit;
        }
        echo json_encode(array('r' => -1));
        exit;
    }


   function allowplay(){
        $post=$_POST;
        $v=$post['i'];
       $allow_play['value'] = 0;
        $allow_play=DB::fetch_first("select `value` from ".DB::table('setting')." where name='game_v".$v."'");
        if (!empty($allow_play['value']) && 1 == intval($allow_play['value'])) {
           $allow_play['value'] = 1;
       }

      echo json_encode(array('r'=>0,'allowplay'=>$allow_play['value']));
   }

function game_money(){
     $game_money=DB::fetch_first("select `value` from ".DB::table('setting')." where name='game_money'");
     echo $game_money['value'];exit;
}



}
?>